<?php
/**
 * Created by PhpStorm.
 * 售后工单
 * User: ciacm
 * Date: 2017/5/19
 * Time: 13:53
 */

namespace SDXApi\Model;


use Think\Model;

class WorkorderModel extends Model {

    /*
     * 获取售后工单详情
     * Created By caicm
     * @param int $uid 用户id
     * @param int $order_id 订单id
     * @param int $goods_id  商品id
     * @param int $workorder_id 工单id
     * return mixed
     */
    public function get_workorder_info($uid=0, $order_id=0, $goods_id=0, $workorder_id=0){

        $where = array(
            'uid' => $uid,
            'order_id' => $order_id,
            'goods_id' => $goods_id,
            'id' => $workorder_id,
            'status' => array('neq',4)
        );

        if($order_id == 0) unset($where['order_id']);
        if($goods_id == 0) unset($where['goods_id']);
        if($workorder_id == 0) unset($where['id']);

        return $this->where($where)->find();

}

    /*
     * 获取售后工单详情(根据工单id)
     * Created By caicm
     * @param int $uid 用户id
     * @param int $order_id 订单id
     * @param int $goods_id  商品id
     * @param int $workorder_id 工单id
     * @param int $is_cancel 是否过滤取消状态 0：否，1：是
     * return mixed
     */
    public function get_workorder_info_by_workorderid($uid=0, $workorder_id=0){

        $where = array(
            'w.uid' => $uid,
            'w.id' => $workorder_id
        );

        $field = 'w.is_reply,w.is_overlay,o.add_time,w.id as workroder_id,w.order_sn,w.status,og.goods_id,og.goods_number,og.giving,og.goods_price,g.goods_name,g.goods_thumb,g.goods_img,g.cat_id';

        return  $this
            ->alias('w')
            ->join('left join sdx_order_goods as og on w.order_id=og.order_id and w.goods_id=og.goods_id')
            ->join('left join sdx_goods as g on og.goods_id=g.goods_id')
            ->join('left join sdx_order_info as o on w.order_id=o.order_id')
            ->field($field)
            ->where($where)
            ->find();

    }

    /*
     * 获取售后工单详情数量
     * Created By caicm
     * @param int $uid 用户id
     * @param int $order_id 订单id
     * @param int $goods_id  商品id
     * return mixed
     */
    public function get_workorder_count($uid=0, $order_id=0, $goods_id=0){

        $where = array(
            'uid' => $uid,
            'order_id' => $order_id,
            'goods_id' => $goods_id,
            'status' => array('neq',4)
        );

        return $this->where($where)->count();

    }


    /**
     * 获取工单申请记录数量
     * Created By caicm
     * @param int $uid
     * @param string $keyword
     * @return mixed
     */
    public function get_workorder_apply_record_count($uid=0, $keyword=''){

        $where = array(
            'uid' => $uid
        );

        if($keyword){
            $where['_string'] = " (order_sn like '%{$keyword}%') or (goods_name like '%{$keyword}%')";
        }

        return $this->where($where)->count('id');

    }

    /**
     * 获取工单申请记录列表
     * Created By caicm
     * @param int $uid
     * @param string $keyword
     * @param string $limit
     * @return mixed
     */
    public function get_workorder_apply_record($uid=0, $keyword='', $limit=''){

        $where = array(
            'w.uid' => $uid
        );

        if($keyword){
            $where['_string'] = " (w.order_sn like '%{$keyword}%') or (w.goods_name like '%{$keyword}%')";
        }

        $field = 'if(w.status >3,0,1) as num,w.add_time,w.id as workroder_id,w.order_sn,w.status,og.goods_id,og.goods_number,og.giving,og.goods_price,g.goods_name,g.goods_thumb,g.goods_img,g.cat_id';

        return  $this
            ->alias('w')
            ->join('left join sdx_order_goods as og on w.order_id=og.order_id and w.goods_id=og.goods_id')
            ->join('left join sdx_goods as g on og.goods_id=g.goods_id')
            ->field($field)
            ->where($where)
            ->limit($limit)
            ->group('w.id')
            ->order('num desc,w.id desc')
            ->select();

    }

    /*
     * 获取工单订单列表数量
     * Created By caicm
     * @param int $uid 用户id
     * @param string $keyword 关键词（订单号、商品名称）
     * @return mixed
     */
    public function get_order_count($uid=0, $keyword=''){

        $where = array(
            'o.user_id' => $uid,
            'o.shipping_status' =>2,
            'o.order_status' => array('in', array(1,5))
        );

        if($keyword){
            $where['_string'] = " (o.order_sn like '%{$keyword}%') or (og.goods_name like '%{$keyword}%')";
        }

        return M('order_info')
            ->alias('o')
            ->field('o.order_id')
            ->join('left join sdx_order_goods as og on o.order_id=og.order_id')
            ->where($where)
            ->count('DISTINCT o.order_id');

    }

    /*
     * 获取工单订单列表
     * Created By caicm
     * @param int $uid 用户id
     * @param string $keyword 关键词（订单号、商品名称）
     * @param int $limit 分页
     * @return mixed
     */
    public function get_order_list($uid=0, $keyword='', $limit=10) {

        $where = array(
            'o.user_id' => $uid,
            'o.shipping_status' => 2,
            'o.order_status' => array('in', array(1, 5))
        );

        if ($keyword) {
            $where['_string'] = " (o.order_sn like '%{$keyword}%') or (og.goods_name like '%{$keyword}%')";
        }

        $field = 'o.order_id,o.order_sn,o.add_time,o.confirm_time';

         M('order_info')
            ->alias('o')
            ->field($field)
            ->join('left join sdx_order_goods as og on o.order_id=og.order_id')
            ->where($where)
            ->group('o.order_id')
            ->order('o.order_id desc')
            ->limit($limit)
            ->select();

    }

    /*
     * 获取工单订单详情
     * Created By caicm
     * @param int $uid 用户id
     * @param int $order_id 订单id
     * @return mixed
     */
    public function get_order_info($uid=0, $order_id=0){

        $where = array(
            'order_id'=>$order_id,
            'user_id'=>$uid
        );

        $field = 'pay_status,shipping_status,order_status,order_id,order_sn,add_time,confirm_time';

        return M('order_info')->field($field)->where($where)->find();

    }

    /*
     * 获取工单订单商品列表
     * Created By caicm
     * @param int $order_id 订单id
     * @param  $limit
     * @return mixed
     */
    public function get_order_goods($order_id=0, $limit=''){

        $where = array(
            'og.order_id' => $order_id
        );

        $field = 'og.goods_id,og.goods_number,og.giving,og.goods_price,g.goods_name,g.goods_thumb,g.goods_img,g.cat_id';

        return  M('order_goods')
            ->alias('og')
            ->join('left join sdx_goods as g on og.goods_id=g.goods_id')
            ->field($field)
            ->where($where)
            ->limit($limit)
            ->select();

    }

    /*
     * 获取工单订单商品列表数量
     * Created By caicm
     * @param int $order_id 订单id
     * @return mixed
     */
    public function get_order_goods_count($order_id=0){

        $where = array(
            'order_id' => $order_id
        );

        return  M('order_goods')->where($where)->count();

    }

    /*
     * 获取工单订单商品列表
     * Created By caicm
     * @param int $order_id 订单id
     * @param int $goods_id 商品id
     * @return mixed
     */
    public function get_order_goods_info($order_id=0, $goods_id=0){

        $where = array(
            'og.order_id' => $order_id,
            'og.goods_id' => $goods_id
        );

        $field = 'og.goods_id,og.goods_number,og.giving,og.goods_price,g.goods_name,g.goods_thumb,g.goods_img,g.cat_id';

        return  M('order_goods')
            ->alias('og')
            ->join('left join sdx_goods as g on og.goods_id=g.goods_id')
            ->field($field)
            ->where($where)
            ->find();

    }

    /*
     * 添加工单
     * Created By caicm
     * @param int $uid 用户id
     * @param string $user_name
     * @param int $order_id 订单id
     * @param string $order_sn 订单号
     * @param int $goods_id 商品id
     * @param string $goods_name 商品名称
     * @param int $cause 原因
     * @param int $dispose_way 处理方式
     * @param int $is_overlay 追加描述：0否，1是
     * @return mixed
     */
    public function add_workorder($uid=0, $user_name='', $order_id=0, $order_sn='', $goods_id=0, $goods_name='', $cause=0, $dispose_way=0, $is_overlay=0){

        $data = array(
            'uid' => $uid,
            'user_name' => $user_name,
            'order_id' => $order_id,
            'order_sn' => $order_sn,
            'goods_id' => $goods_id,
            'goods_name' => $goods_name,
            'status' => 1,
            'cause' => $cause,
            'dispose_way' => $dispose_way,
            'is_overlay' => $is_overlay,
            'is_look' => 0,
            'version' => I('post.version') ,
            'source' => get_source() ,
            'add_time' => gmtime()
        );

        return $this->data($data)->add();

    }

    /**
 * 修改工单表追加描述
 * Created By caicm
 * @param int $workorder_id
 * @return bool
 */
    public function update_workorder_overlay($workorder_id=0){

        $where = array(
            'id' => $workorder_id
        );

        $data = array(
            'is_overlay' => 1,
            'status' => 1,
            'is_look' => 0,
            'update_time' => gmtime()
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 修改工单为已读
     * Created By caicm
     * @param int $workorder_id
     * @return bool
     */
    public function update_workorder_is_look($workorder_id=0){

        $where = array(
            'id' => $workorder_id
        );

        $data = array(
            'is_look' => 1
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 取消工单
     * Created By caicm
     * @param int $workorder_id
     * @param int $status
     * @return bool
     */
    public function cancel_workorder($workorder_id=0, $status=4){

        $where = array(
            'id' => $workorder_id
        );

        $data = array(
            'status' => $status,
            'update_time' => gmtime()
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * @param int $uid
     * @return mixed
     */
    public function get_workorder_number($uid=0){

        return $this->where("(status = 1 or (status > 1 and is_look=0)) and uid = {$uid}")->count('id');
    }

    /**
     * 获取可以关闭工单列表
     * @param int $close_day
     * @return mixed
     */
    public function get_close_workorder($close_day=7){

        //关闭时间
        $close_time = $close_day * 86400;
        $time = gmtime();
        $where = "status = 2 and update_time+{$close_time} < {$time}";

        return $this->field('id')->where($where)->select();

    }

    /**
     * 获取商品退款总数
     * @param int $order_id
     * @param int $goods_id
     * @return mixed
     */
    public function get_goods_refund_num($order_id=0, $goods_id=0){

        $where = array(
            'order_id' => $order_id,
            'goods_id' => $goods_id
        );

        return M('refund_detail')->where($where)->sum('refund_number');
    }

}